layui.use(['form', 'element', 'util', 'carousel', 'laypage', 'layer','table'], function () {
    var table = layui.table;
    table.render({
        elem: '#collect'
        , url: basePath+'/user/cart/queryall'
        ,toolbar: '#toolbarDemo'
        , page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
            , groups: 3
            , limits: [20, 50, 100]
            , limit: 20
        }, cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'qid', title: 'ID',width:60, align:'center'},
            {field: 'cid', title: '购物车id',width:60, align:'center',hide:true}
            , {field: 'cname', title: '商品名称', width: 350, align:'center'}
            , {field: 'price', title: '价格', width: 150, align:'center', templet: '<div>￥{{d.price}}元</div>'}
            , {field: 'cNum', title: '数量', width: 110, align:'center', templet: '<div><input type="button" onclick="decrease({{d.cNum}},{{d.cid}})" class="decrease" value="   -   ">\n' +
                    '                    {{d.cNum}}\n' +
                    '                    <input type="button" onclick="increase({{d.cNum}},{{d.cid}})" class="increase" value="    +    "></div>'}
            , {field: 'cCount', title: '小计', width: 130, align:'center', templet: '<div>￥{{d.cCount}}元</div>'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width:140, align:'center'}
        ]]
        ,height: 500
        , done: function (res, curr, count) {
            var i=1;
            $("[data-field='qid']").children().each(function () {
                if($(this).text() == 'ID') {
                    $(this).text("ID")
                }else{
                    $(this).text(i++)
                }
            });
        }
    });
    $(".increase");
    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'xiangqing') {
            $.ajax({
                url: basePath+'/product-detail-isnull/'+data.commid,
                data: "",
                contentType: "application/json;charset=UTF-8", //发送数据的格式
                type: "get",
                dataType: "json", //回调
                beforeSend: function () {
                    layer.load(1, { //icon支持传入0-2
                        content: '请稍等...',
                        success: function (layero) {
                            layero.find('.layui-layer-content').css({
                                'padding-top': '39px',
                                'width': '60px'
                            });
                        }
                    });
                },
                complete: function () {
                    layer.closeAll('loading');
                },
                success: function (shuju) {
                    console.log(shuju)
                    if(shuju.status===200){
                        window.open(basePath+"/product-detail/"+data.commid)
                    }else {
                        layer.msg(shuju.message, {
                            time: 1000,
                            icon: 2,
                            offset: '50px'
                        });
                    }
                }
            });
        }else if(obj.event === 'quxiaoshoucang'){
            layer.confirm('确认取消收藏该商品吗？', {
                btn: ['确定','算了'], //按钮
                title:"取消收藏",
                offset:"50px"
            }, function(){
                layer.closeAll();
                $.ajax({
                    url: basePath+'/cart/delete/'+data.cid,
                    data: "",
                    contentType: "application/json;charset=UTF-8", //发送数据的格式
                    type: "put",
                    dataType: "json", //回调
                    beforeSend: function () {
                        layer.load(1, { //icon支持传入0-2
                            content: '请稍等...',
                            success: function (layero) {
                                layero.find('.layui-layer-content').css({
                                    'padding-top': '39px',
                                    'width': '60px'
                                });
                            }
                        });
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.status===200){
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 1,
                                offset: '50px'
                            }, function () {
                                location.reload();
                            });
                        }else {
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 2,
                                offset: '50px'
                            });
                        }
                    }
                });
            }, function(){
            });
        }
    });

    //头工具栏事件
    //去结算方法
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
        var goods = [];
        var data;
        switch(obj.event){
            case 'getCheckData':
                data = checkStatus.data;  //获取选中行数据
                layer.alert(JSON.stringify(data));
                //break;
        };
        console.log(data);
        for (var i = 0; i < data.length; i++){
            var good = {};
            good["num"] = data[i].cNum;
            console.log(data[i].cNum);
            good["cid"] = data[i].cid;
            goods.push(good);
        }

        var jsonData = JSON.stringify(goods);
        console.log("提交的数据"+jsonData)
        $.ajax({
            url: basePath + "/topay",
            data: jsonData,
            contentType: "application/json;charset=UTF-8", //发送数据的格式
            type: "POST",
            dataType: "json", //回调
            beforeSend: function () {
                layer.load(1, { //icon支持传入0-2
                    content: '提交中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            },
            complete: function () {
                layer.closeAll('loading');
            },
            success: function (data) {
                if (data.status === 200) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 1,
                        offset: '100px'
                    }, function () {
                        window.location.href = basePath + "/common/pay-order"
                    });
                }else {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 5,
                        offset: '100px'
                    });
                }
            },error:function () {
                layer.msg("系统错误", {
                    time: 1000,
                    icon: 2,
                    offset: '100px'
                });
            }
        });

    });

});
function decrease(num, cid) {
    console.log("调用减少方法-------" + num)

    if(num === 1){
        layer.confirm('确认取消删除该商品吗？', {
            btn: ['确定','算了'], //按钮
            title:"取消删除",
            offset:"50px"
        }, function(){
            layer.closeAll();
            $.ajax({
                url: basePath+'/cart/delete/'+cid,
                data: "",
                contentType: "application/json;charset=UTF-8", //发送数据的格式
                type: "put",
                dataType: "json", //回调
                beforeSend: function () {
                    layer.load(1, { //icon支持传入0-2
                        content: '请稍等...',
                        success: function (layero) {
                            layero.find('.layui-layer-content').css({
                                'padding-top': '39px',
                                'width': '60px'
                            });
                        }
                    });
                },
                complete: function () {
                    layer.closeAll('loading');
                },
                success: function (data) {
                    console.log(data)
                    if(data.status===200){
                        layer.msg(data.message, {
                            time: 1000,
                            icon: 1,
                            offset: '50px'
                        }, function () {
                            location.reload();
                        });
                    }else {
                        layer.msg(data.message, {
                            time: 1000,
                            icon: 2,
                            offset: '50px'
                        });
                    }
                }
            });
        }, function(){
        });
    } else {
        num--;
        $.ajax({
            url: basePath + "/user/cart/update/" + num +"/"+ cid,
            contentType: "application/json;charset=UTF-8", //发送数据的格式
            type: "post",
            dataType: "json", //回调
            beforeSend: function () {
                layer.load(1, { //icon支持传入0-2
                    content: '提交中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            },
            complete: function () {
                layer.closeAll('loading');
            },
            success: function (data) {
                if (data.status == 200) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 1,
                        offset: '100px'
                    },function () {
                        location.reload();
                    });
                }else {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 5,
                        offset: '100px'
                    });
                }
            },error:function () {
                layer.msg("系统错误", {
                    time: 1000,
                    icon: 2,
                    offset: '100px'
                });
            }
        });
    }

}

function increase(num, cid) {
    console.log("调用增加方法-------" + num + cid)
    num++;
    $.ajax({
        url: basePath + "/user/cart/update/" + num +"/"+ cid,
        contentType: "application/json;charset=UTF-8", //发送数据的格式
        type: "post",
        dataType: "json", //回调
        beforeSend: function () {
            layer.load(1, { //icon支持传入0-2
                content: '提交中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '39px',
                        'width': '60px'
                    });
                }
            });
        },
        complete: function () {
            layer.closeAll('loading');
        },
        success: function (data) {
            if (data.status == 200) {
                layer.msg(data.message, {
                    time: 1000,
                    icon: 1,
                    offset: '100px'
                },function () {
                    location.reload();
                });
            }else {
                layer.msg(data.message, {
                    time: 1000,
                    icon: 5,
                    offset: '100px'
                });
            }
        },
        error:function () {
            layer.msg("系统错误", {
                time: 1000,
                icon: 2,
                offset: '100px'
            });
        }
    });
}

